/**
 * 与校验相关的API接口
 * @author guocailee
 */
import Vue from 'vue'
import md5 from 'js-md5'

/**
 * 校验用户名是否被使用
 * @param  {[type]} userName [description]
 * @return {[type]}          [description]
 */
export const checkUser = (userName) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/checkUser', {
      username: userName
    }).then(data => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

/**
 * 校验图片验证码
 * @param  {[type]} code [description]
 * @return {[type]}      [description]
 */
export const checkImgCode = (code) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/checkverifycode', {
      code: code
    }).then((data) => {
      if (data.body.payload.status) {
        resolve()
      } else {
        reject('您输入的验证码有误')
      }
    }).catch(data => {
      reject(data)
    })
  })
}

/**
 * 发送注册短信验证码
 * @param String: username 用户名(手机)
 * @return Poomise
 */
export const sendsmsregistercaptcha = (username) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/sendsmsregistercaptcha', {
      username: username
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

/**
 * 校验注册短信验证码是否正确
 * @param Object: account
 * @return Poomise
 */
export const verifysmsregistercaptcha = (account) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/verifysmsregistercaptcha', {
      username: account.username,
      captcha: md5(account.dyCode)
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

/**
 * 发送密码变更验证码
 * @param  {[type]} forgetEmailTemp [description]
 * @return {[type]}                 [description]
 */
export const sendsmschangepwdcaptcha = (forgetEmailTemp) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/sendsmschangepwdcaptcha', {
      forgetEmailTemp: forgetEmailTemp
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

/**
 * 验证密码变更验证码
 * @param  {[type]} forgetEmailTemp [description]
 * @return {[type]}                 [description]
 */
export const verifysmschangepwdcaptcha = (forgetEmailTemp) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/verifysmschangepwdcaptcha', {
      forgetEmailTemp: forgetEmailTemp
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

/**
 * 发送邮件绑定验证码
 * @param  {[type]} forgetEmailTemp [description]
 * @return {[type]}                 [description]
 */
export const sendmailbindcaptcha = (email) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/sendmailbindcaptcha', {
      email: email
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

/**
 * 验证邮件绑定验证码
 * @param  {[type]} forgetEmailTemp [description]
 * @return {[type]}                 [description]
 */
export const verifymailbindcaptcha = (params) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/verifymailbindcaptcha', {
      email: params.newEmail,
      captcha: md5(params.chkCode)
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

export const sendmobilebindcaptcha = (mobile) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/sendmobilebindcaptcha', {
      username: mobile
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}
export const verifymobilebindcaptcha = (params) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/verifymobilebindcaptcha', {
      username: params.newMobile,
      captcha: md5(params.chkCode)
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}

export const sendMailForgetPwdCaptcha = (forgetEmailTemp) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/sendmailforgetpwdcaptcha', {
      forgetEmailTemp: forgetEmailTemp
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}
export const verifyMailForgetPwdCaptcha = (params) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/verifymailforgetpwdcaptcha', {
      forgetEmailTemp: params.forgetEmailTemp,
      captcha: md5(params.chkCode)
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}
export const sendSmsForgetPwdCaptcha = (forgetMobileTemp) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/sendsmsforgetpwdcaptcha', {
      forgetMobileTemp: forgetMobileTemp
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}
export const verifyForgetPwdCaptcha = (params) => {
  return new Promise((resolve, reject) => {
    Vue.fetch.post('account/verifyforgetpwdcaptcha', {
      forgetMobileTemp: params.forgetMobileTemp,
      captcha: md5(params.chkCode)
    }).then((data) => {
      if (data.body.code === '00000000') {
        resolve()
      } else {
        reject(data.body.message)
      }
    }).catch(data => {
      reject(data)
    })
  })
}
